package com.oa.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.common.result.Result;
import com.oa.model.system.SysRole;
import com.oa.system.service.SysRoleService;
import com.oa.vo.system.AssginRoleVo;
import com.oa.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Admin
 * @title: SysRoleController
 * @projectName Cloud_OA_Office
 * @description: SysRoleController
 * @date 2024/10/1 19:54
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole") //访问路径
public class SysRoleController {
    @Autowired
    //注入Service
    private SysRoleService sysRoleService;

    //查询所有角色
    //@GetMapping("/findAll")
    //public List<SysRole> findAll(){
    //    return sysRoleService.list();
    //    //List<SysRole> list = sysRoleService.list();
    //    //return list;
    //}
    @ApiOperation(value = "查询所有角色")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAll() {
        return Result.ok(sysRoleService.list());
    }

    /**
     * 条件分页查询
     */
    @ApiOperation(value = "条件分页查询")
    //@PreAuthorize("hasAuthority('bnt.sysRole.list')")
    //@GetMapping("/pageList/{current}/{limit}")
    //page当前页  limit 每页显示条数  SysRoleQueryVo 条件对象
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page, @PathVariable Long limit, SysRoleQueryVo sysRoleQueryVo) {
        //调用service方法实现
        //1 创建Page对象 传递分页相关参数
        Page<SysRole> pageParam = new Page<>(page, limit);

        //2 判断条件 判断条件是否为空  不为空进行封装
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)) { //StringUtils  来自spring包下
            wrapper.like(SysRole::getRoleName, roleName);
        }
        //3 调用service方法实现分页查询
        IPage<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    /**
     * 添加角色
     *
     * @param sysRole
     * @return
     */
    @ApiOperation("添加角色")
    //@PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole) {
        //调用service方法实现
        boolean is_success = sysRoleService.save(sysRole);
        if (is_success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据id查询角色
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询角色")
    //@PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("/get/{id}")
    public Result getRole(@PathVariable Long id) {
        return Result.ok(sysRoleService.getById(id));
        //SysRole sysRole = sysRoleService.getById(id);
        //return Result.ok(sysRole);
    }

    /**
     * 根据id修改角色
     *
     * @param sysRole
     * @return
     */
    //@PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result update(@RequestBody SysRole sysRole) {
        //调用service方法实现
        boolean is_success = sysRoleService.updateById(sysRole);
        if (is_success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据id删除角色
     *
     * @param id
     * @return
     */
    //@PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        //调用service方法实现
        boolean is_success = sysRoleService.removeById(id);
        if (is_success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    //@PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    //前端传参 一般为json对象 或json数组  在Java中会转换为 java对象 和 List集合
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean is_success = sysRoleService.removeByIds(idList);
        if (is_success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String,Object> roleMap = sysRoleService.findRoleByAdminId(userId);
        return Result.ok(roleMap);
    }
    @ApiOperation(value = "给用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
}
